<template>
  <div class="wrapper">
    <div class="box">
      <div class="logo">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <div class="form">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="form.username"
              style="width: 280px"
              prefix-icon="el-icon-s-custom"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              style="width: 280px"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <el-form-item class="button">
            <el-button type="primary" @click="login" :loading="loading"
              >登录</el-button
            >
            <el-button type="info" @click="rest">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login, getMenus } from "api/user";
import { Message } from "element-ui";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    async login() {
      try {
        this.$refs.form.validate();
        this.loading = true;
        const data = await login(this.form);
        this.$store.commit("setUsers", data);
        this.$router.push("/");
        const menus = await getMenus();
        this.$store.commit("setMenus", menus);
        this.$router.push({ name: "Home" });
        Message.success("登录成功");
      } catch (error) {}
      this.loading = false;
    },
    async rest() {
      this.$refs.form.resetFields();
      this.form = {};
    },
  },
};
</script>

<style scoped lang="scss">
.wrapper {
  height: 100%;
  background: var(--mainBackColor);
  position: relative;
  .box {
    width: 450px;
    height: 360px;
    background: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .form {
      margin-top: 120px;
    }
    .button {
      position: absolute;
      right: 100px;
    }
    .logo {
      width: 152px;
      height: 152px;
      border-radius: 100%;
      border: 11px solid #fff;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #fff;
      img {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background: #eee;
      }
    }
  }
}
</style>
